package com.ideal.service.access.impl;

import com.ideal.service.access.ExeclAccessService;
import com.ideal.service.cache.ExeclCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author songfz
 * @Description Execl 文件内容数据业务处理类
 * @Date 2022/8/4
 */
@Service
public class ExeclAccessServiceImpl implements ExeclAccessService {

    @Autowired
    private ExeclCacheService execlCacheService;

    /**
     * 遍历execl每个sheet行列内容，通过redis list 添加每个列数据
     * @param fileEnName  execl文件唯一标识
     * @param context 文件内容
     * @return
     */
    @Override
    public boolean addExeclDataCache(String fileEnName, List<Map<String, List<List<String>>>> context){

        //转换对象

        for (int i = 0; i < context.size(); i++) {
            final Map<String, List<List<String>>> map = context.get(i);

            for (Map.Entry<String, List<List<String>>> entry : map.entrySet()) {
                // sheet页
                final String key = entry.getKey();
                final List<List<String>> rowContents = entry.getValue();

                for (int i1 = 0; i1 < rowContents.size(); i1++) {

                    final List<String> rows = rowContents.get(i1);
                    for (int i2 = 1; i2 < rows.size(); i2++) {

                        execlCacheService.addCell(fileEnName,i,i2,rows.get(i2));
                    }
                }
            }

        }

        return true;
    }



    /**
     *根据条件获取execl文档内容
     * @param fileEnName
     * @param sheet 第几页
     * @param startRow 开始行号
     * @param params  获取文件内容条件（列）
     * @return
     */
    @Override
    public List<Map> getValue(String fileEnName, Integer sheet, Long startRow, Map<String, Integer> params) {

        final List<Map> result = new ArrayList<>();

        Integer size = 0;
        //列行数据
        final Map<String, List> contentMap = new HashMap<>();
        for (Map.Entry<String, Integer> entry : params.entrySet()) {
            Long startRowTemp = startRow;
            final String key = entry.getKey();
            final Integer value = entry.getValue();

            final List<String> cellContents = execlCacheService.getCellContents(fileEnName, sheet, value);

            final Iterator<String> iterator = cellContents.iterator();
            while (iterator.hasNext()) {
                iterator.next();
                --startRowTemp;
                if (startRowTemp > 0) {
                    iterator.remove();
                }else {
                    break;
                }
            }
            size =size > cellContents.size() ? size : cellContents.size();
            contentMap.put(key,cellContents);
        }


        //列行数据转换为 行列数据

        for(int i = 0 ;i<size; ++i){

            final Map<String, String> colMap = new HashMap<>(16);
            for (Map.Entry<String, List> listEntry : contentMap.entrySet()) {
                final String key = listEntry.getKey();

                final List<String> contentList = listEntry.getValue();
                colMap.put(key,contentList.get(i));
            }
            result.add(colMap);
        }
        return result;
    }


    /**
     * 模糊删除所有key内容
     * @param fuzzyKey
     */
    @Override
    public void fuzzyDelKeyAll(String fuzzyKey) {

        execlCacheService.fuzzyDelKeyAll(fuzzyKey);
    }

    public static void main(String[] args) {

        final List<Map> maps = new ArrayList<>();
        final HashMap<String, Integer> map = new HashMap<>();
        map.put("id",1);
        map.put("name",2);

        for (int i = 1; i< 5;++i){
            final HashMap<String, String> rowData = new HashMap<>();
            Iterator<String> iterator = map.keySet().iterator();

            for (Map.Entry<String, Integer> entry : map.entrySet()) {
                final String key = entry.getKey();
                final Integer col = entry.getValue();
                String value = "ss"+i;
                rowData.put(key, value);
            }
            while (iterator.hasNext()) {
                final String key = iterator.next();
                final Integer col = map.get(key);
                System.out.println(key);

                //String value = "ss"+i;
                //rowData.put(key, value);
            }
            maps.add(rowData);
        }


        System.out.println(maps);
    }
}
